package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyzy.mapper.HitCarMapper;
import com.cyzy.pojo.CarModel;
import com.cyzy.service.FareStrategy;
import com.cyzy.service.HitCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalTime;
import java.util.List;

/**
 * @author cuncun
 * @create 2025-05-28 上午9:18
 */
@Service
public class HitCarServiceImpl extends ServiceImpl<HitCarMapper, CarModel> implements HitCarService  {


    @Autowired
    private HitCarMapper hitCarMapper;

    @Autowired
    private FareStrategyContext fareStrategyContext;

    @Override
    public List<CarModel> getModelName() {

        return hitCarMapper.getModelName();
    }


    @Override
    public List<CarModel> getCarTypes(double kilometer) {

        List<CarModel> carModels = hitCarMapper.selectList(new LambdaQueryWrapper<>());
        System.out.println(carModels + " ------35");

        //计算时间是否是夜间
        Boolean nightTime = isNightTime(LocalTime.now());

        //给每个车型计算价格
        for (CarModel carModel : carModels) {


            //拿到当前车型的计费规则
            FareStrategy fareStrategy = fareStrategyContext.getFareStrategy(nightTime);

            //开始计算价格
            double money = fareStrategy.calculateFare(carModel, kilometer);

            //仅保留两位小数
            System.out.println(money + " money");
            BigDecimal bd = new BigDecimal(money);
            bd = bd.setScale(2, RoundingMode.HALF_UP); // 四舍五入保留两位小数
            double result = bd.doubleValue(); // 返回 double 类型
            System.out.println(result + "----");
            //赋值
            carModel.setMoney(result);
        }

        return carModels;
    }

    //判断当前时间是在夜间（23-5）还是白天
    private Boolean isNightTime(LocalTime time){

        LocalTime nightStart = LocalTime.of(23,0); //晚上11点
        LocalTime NightEnd = LocalTime.of(5,0); //凌晨5点

        // 如果当前时间在23:00之后或者在05:00之前，则认为是夜间
        return !time.isBefore(nightStart) || time.isBefore(NightEnd);
    }
}
